<?php
	class BaseAction extends Action{
		protected $is_log;//是否登录
		protected $name;//登录账号名
		protected $user_id;//登录账号id
		//显示导航
		function _initialize(){
// 			header("Content-Type:text/html; charset=utf-8");
			$cat = M('Category');
			$nav = $cat->field('pid,id,category_name')
						->where('pid = 0')
						->order('sort,id')
						->limit(C('NAV_NUM'))
						->select();
			$this->assign('nav',$nav);
			//检查是否登录
			$this->authCookie();
			$this->assign('is_log',$this->is_log);
// 			$this->assign('name',$this->name);

			//显示友情链接,页脚,文字链接和图片链接各十条
			$link = M('Link');
			//文字链接
			$tenTextLink = $link->field('webname,weburl')
							->limit(10)
							->where('type = 1 and state = 1')
							->select();
			$this->assign('tenTextLink',$tenTextLink);
			//图片链接
			$tenLogoLink = $link->field('webname,weburl,logourl')
							->limit(10)
							->where('type = 2 and state = 1')
							->select();
			$this->assign('tenLogoLink',$tenLogoLink);
			
			//显示五条热门搜索tag标签
			$tag = M('Tag');
			$tagData = $tag->field(true)
							->order('count desc')
							->limit(5)
							->select();
			//网站名称
			$this->assign('webname',C('WEB_NAME'));
			$this->assign('tag',$tagData);
			
		}
		
		//检查cookie是否正确
		protected function authCookie(){
			$user = M('User');
			$data = $user->field('id,user,face')
						->getByUser(cookie('name'));
			//判断是否存在相应的用户，并且验证cookie是否有伪造。
			if($data && cookie('auth') == md5($data['user'].'zgcms')){
				$this->name = cookie('name');
				$this->user_id = $data['id'];
				$this->assign('name',$this->name);
				$this->assign('user_id',$this->user_id);
				//echo $data['face'];
				$this->assign('face',$data['face']);
				$this->is_log = true;
				return true;
			}else{//验证不通过则销毁cookie
				cookie('name',null);
				cookie('auth',null);
				$this->is_log = false;
				return false;
			}
		}
	}
?>